﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace GTA_V_Handling_Editor
{
    public class Utils
    {
        public static string GetNameFromLine(string line)
        {
            line = line.Substring(firstcharposinstring(line));
            return line.Substring(0, line.IndexOf(' '));
        }
        public static Data GetDataFromLine(string line)
        {
            line = line.Substring(firstcharposinstring(line));
            return new Data(line.Substring(0, line.IndexOf(' ')), line.Substring(line.IndexOf(' ')).Split(new string[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries));
        }
        public static Data GetDataFromName(string name, Data[] DataArray)
        {
            foreach (Data data in DataArray)
            {
                if (data.Name == name)
                    return data;
            }
            throw new Exception("Vehicle Not Found");
        }

        public static int firstcharposinstring(string data)
        {
            char letter;
            for (int i = 0; i < data.Length; i++)
            {
                letter = data[i];
                if ((letter >= 'A' && letter <= 'Z') || (letter >= 'a' && letter <= 'z')) return i;
            }
            return -1;
        }

        public static string returnarraytostring(string[] array, string seperator)
        {
            if (array.Length < 1) return "";
            string value;
            value = array[0];
            for (int i = 1; i <= array.GetUpperBound(0); i++)
            {
                value += seperator + array[i];
            }
            return value;
        }

        public static void save(string filename, MemoryStream FileData, HandlingData Vehicles)
        {
            FileData.Position = 0;
            StreamReader stream = new StreamReader(FileData, Encoding.Default);
            StreamWriter savefile = new StreamWriter(new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write), stream.CurrentEncoding);
            string line, fline, name;
            while (!stream.EndOfStream)
            {
                line = stream.ReadLine();
                fline = line.Replace("\t", " ").Replace("  ", " ");
                if (fline.Length >= 1)
                {
                    char first = line[0];
                    if ((first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z'))
                    {
                        name = Utils.GetNameFromLine(fline);
                        line = name + " \t" + Utils.returnarraytostring(Utils.GetDataFromName(name, Vehicles.Cars).Handling, " \t");
                    }
                    else
                    {
                        Data[] temp = null;
                        bool dataline = true;
                        switch (first)
                        {
                            case '%': temp = Vehicles.Boats; break;
                            case '!': temp = Vehicles.Bikes; break;
                            case '$': temp = Vehicles.Flying; break;
                            case '£': temp = Vehicles.Weapons; break;
                            case '*': temp = Vehicles.Submarines; break;
                            case '&': temp = Vehicles.Trains; break;
                            case '@': temp = Vehicles.Trailers; break;
                            case '^': temp = Vehicles.Convertables; break;
                            default: dataline = false; break;
                        }
                        if (dataline)
                        {
                            name = Utils.GetNameFromLine(fline);
                            line = first + " \t" + name + " \t" + Utils.returnarraytostring(Utils.GetDataFromName(name, temp).Handling, " \t");
                        }
                    }
                }
                savefile.WriteLine(line);
            }
            savefile.Close();
            MessageBox.Show("File Saved");
        }

        public static HandlingData GetVehicles(MemoryStream FileData)
        {
            FileData.Position = 0;
            StreamReader stream = new StreamReader(FileData, Encoding.Default);
            string line;
            List<Data> Cars = new List<Data>();
            List<Data> Boats = new List<Data>();
            List<Data> Bikes = new List<Data>();
            List<Data> Flying = new List<Data>();
            List<Data> Weapons = new List<Data>();
            List<Data> Subs = new List<Data>();
            List<Data> Trains = new List<Data>();
            List<Data> Trailers = new List<Data>();
            List<Data> Convertables = new List<Data>();
            while (!stream.EndOfStream)
            {
                line = stream.ReadLine();
                line = line.Replace("\t", " ").Replace("  ", " ").Replace("	", " ");
                if (line.Length < 1) continue;
                char first = line[0];
                if ((first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z'))
                {
                    Cars.Add(Utils.GetDataFromLine(line));
                }
                else
                {
                    switch (first)
                    {
                        case '%': Boats.Add(GetDataFromLine(line)); break;
                        case '!': Bikes.Add(GetDataFromLine(line)); break;
                        case '$': Flying.Add(GetDataFromLine(line)); break;
                        case '£': Weapons.Add(GetDataFromLine(line)); break;
                        case '*': Subs.Add(GetDataFromLine(line)); break;
                        case '&': Trains.Add(GetDataFromLine(line)); break;
                        case '@': Trailers.Add(GetDataFromLine(line)); break;
                        case '^': Convertables.Add(GetDataFromLine(line)); break;
                    }
                }
            }
            return new HandlingData(Cars.ToArray(), Bikes.ToArray(), Boats.ToArray(), Flying.ToArray(), Weapons.ToArray(), Subs.ToArray(), Trains.ToArray(), Trailers.ToArray(), Convertables.ToArray());
        }
    }
}
